﻿(function () {
    var appPP = angular.module('popupModule', ['basicFunctions']);

    appPP.controller('setterPopup', function ($scope, Services) {

        $scope.$on('setName', function () {
            if ($scope.dataItem.Id == Services.Name.Id) {
                if (Services.Name.type == 'load') {
                    if ($scope.checkName($scope.dataItem.LoadingPorts, Services.Name.name)) {
                        $scope.dataItem.LoadingPorts[$scope.dataItem.SelectedLoadingPort].Name = Services.Name.name;
                        Services.update({ selected: Services.Name.name, type: Services.Name.type, Id: $scope.dataItem.Id });
                    }
                }
                else
                    if ($scope.checkName($scope.dataItem.DischargePorts, Services.Name.name)) {
                        $scope.dataItem.DischargePorts[$scope.dataItem.SelectedDischargePort].Name = Services.Name.name;
                        Services.update({ selected: Services.Name.name, type: Services.Name.type, Id: $scope.dataItem.Id });
                    }
            }
        });

        $scope.$on('changeSelected', function () {
            if($scope.dataItem.Id==Services.selected.Id)
                if (Services.selected.type == 'load') 
                    $scope.dataItem.SelectedLoadingPort = Services.selected.index;
                else 
                    $scope.dataItem.SelectedDischargePort = Services.selected.index;
        });

        $scope.checkName = function (data, name) {
            if (name == '')
                return false;
            for (var i = 0; i < data.length; i++)
                if (data[i].Name == name) 
                    return false;
            return true;
        };

        $scope.$on('changeOrderDate', function () {
            if ($scope.dataItem.Id == Services.order.Id) {
                switch (Services.order.name) {
                    case 'VOD':
                        $scope.dataItem.VoyageOrderDate = Services.order.date;
                        break;
                    case 'LOD':
                        $scope.dataItem.LoadingOrderDate = Services.order.date;
                        break;
                    case 'DO':
                        $scope.dataItem.DischargeOrderDate = Services.order.date;
                        break;
                    case 'BOD':
                        $scope.dataItem.BillOfLadenDate = Services.order.date;
                        break;
                    case 'LoI':
                        $scope.dataItem.LetterOfIndemnityDate = Services.order.date;
                        break;
                    case 'FID':
                        $scope.dataItem.FreightInvoiceDate = Services.order.date;
                        break;
                    case 'CP':
                        $scope.dataItem.CPDate = Services.order.date;
                        break;
                    case 'LC_F':
                        $scope.dataItem.LayCan.FromDate = Services.order.date;
                        break;
                    case 'LC_T':
                        $scope.dataItem.LayCan.ToDate = Services.order.date;
                        break;
                    case 'OW':
                        $scope.dataItem.Owner = Services.order.date;
                        break;
                    case 'OWOP':
                        $scope.dataItem.OwnerOperator = Services.order.date;
                        break;
                    case 'CH':
                        $scope.dataItem.Charterer = Services.order.date;
                        break;
                    case 'CHOP':
                        $scope.dataItem.ChartererOperator = Services.order.date;
                        break;
                    case 'Zone':
                        if(Services.order.type=='load')
                            $scope.dataItem.LoadingPorts[Services.order.index].Zone = Services.order.date.ZoneName;
                        else
                            $scope.dataItem.DischargePorts[Services.order.index].Zone = Services.order.date.ZoneName;
                        break;
                    case 'Vessel':
                        $scope.dataItem.Vessel.Name = Services.order.date.Name;
                        $scope.dataItem.Vessel.Imo = Services.order.date.Imo;
                        $scope.dataItem.VesselName = Services.order.date.Name;
                        $scope.dataItem.VesselIMO = Services.order.date.Imo;
                        break;
                    case 'name':
                        if (Services.order.type == 'load')
                            $scope.dataItem.LoadingPorts[Services.order.index].Name = Services.order.value;
                        else
                            $scope.dataItem.DischargePorts[Services.order.index].Name = Services.order.value;
                        break;
                    default: break;
                }
                $scope.changed();
            }
        });

        $scope.$on('boxCheck', function () {
            if (Services.box.id == $scope.dataItem.Id)
                $scope.changed();
        });

        $scope.changed = function () {
            $scope.$parent.dataItem.changed = true;;
        };
    });

    appPP.controller('popupLayCan', function ($scope, $filter, Services) {

        $scope.$on('initPopupLay', function () {
            $scope.LayCan = { ToDate: Services.LayCan.LayCan.ToDate, FromDate: Services.LayCan.LayCan.FromDate };
            $scope.LC_T = $scope.getDateString(Services.LayCan.LayCan.ToDate);
            $scope.LC_F = $scope.getDateString(Services.LayCan.LayCan.FromDate);
            $scope.id = Services.LayCan.id;
        });

        $scope.changeOrderDate = function (name) {
            if(name=='LC_T')
                Services.changeOrderDate({ Id: $scope.id, date: $scope.LayCan.ToDate, name: name });
            else
                Services.changeOrderDate({ Id: $scope.id, date: $scope.LayCan.FromDate, name: name });
        };

        $scope.getDateString = function (date) {
            if (date == null)
                return '';
            return $filter('date')(date, 'dd.MM.yyyy');
        };

        $scope.clearDate = function () {
            return null;
        };
    });

    appPP.controller('popupDateTime', function ($scope, $filter, Services) {

        $scope.$on('initPopupDate', function () {
            $scope.name = Services.popup.name;
            $scope.date = Services.popup.date;
            if ($scope.name == 'CP')
                $scope.model = $scope.getDateString(Services.popup.date);
            else
                $scope.model = $scope.getDateTimeString(Services.popup.date);
            $scope.id = Services.popup.id;
        });

        $scope.changeOrderDate = function () {
            Services.changeOrderDate({ Id: $scope.id, date: $scope.date, name: $scope.name });
        };

        $scope.clearDate = function () {
            return null;
        };

        $scope.getDateTimeString = function (date) {
            if (date == null)
                return '';
            return $filter('date')(date, 'dd.MM.yyyy HH:mm');
        };

        $scope.getDateString = function (date) {
            if (date == null)
                return '';
            return $filter('date')(date, 'dd.MM.yyyy');
        };
    });

    appPP.controller('popupController', function ($scope, $filter, Services, $http) {

        $scope.serviceUri = "http://efm.develop.local/api/";

        $scope.$on('initPopup', function () {
            $scope.data = Services.popupPorts.ports[Services.popupPorts.index];
            $scope.index = Services.popupPorts.index;
            $scope.realType = Services.popupPorts.type;
            $scope.selectedIndex = Services.popupPorts.selected;
            if ($scope.selectedIndex != 0)
                $scope.type = Services.popupPorts.type + 'popup';
            else
                $scope.type = Services.popupPorts.type;
            $scope.id = Services.popupPorts.id;
            $scope.hasError = false;
            $scope.ports = Services.popupPorts.ports;
            $scope.length = 3;
            $scope.styleUnloader = { display: 'none' };
            $scope.styleLoader = { display: 'inline-block' };
            $scope.currStyle = $scope.styleUnloader;
            $scope.port = {
                name: $scope.data.Name,
                ETAMarker: $scope.data.ETAMarker,
                AAMarker: $scope.data.AAMarker,
                ETBMarker: $scope.data.ETBMarker,
                ETDMarker: $scope.data.ETDMarker,
                ETCMarker: $scope.data.ETCMarker,
                ETA_NORTime: $scope.getDateTimeString($scope.data.ETA_NORTime),
                AATime: $scope.getDateTimeString($scope.data.AATime),
                ETB_ATBTime: $scope.getDateTimeString($scope.data.ETB_ATBTime),
                ETD_ATDTime: $scope.getDateTimeString($scope.data.ETD_ATDTime),
                SldDate: $scope.getDateTimeString($scope.data.SldDate)
            };
        });

        $scope.getDateTimeString = function (date) {
            if (date == null)
                return '';
            return $filter('date')(date, 'dd.MM.yyyy H:mm');
        };

        $scope.changeMarker = function (name, value) {
            if ($scope.selectedIndex==0)
                Services.updateMarker({ type: $scope.realType, name: name, value: value, Id: $scope.id });
            Services.boxCheck({ id: $scope.id });
        };

        $scope.updateCalc = function (markers, ports) {
            if ($scope.selectedIndex == 0)
                Services.updateCalc({ markers: markers, type: $scope.realType, Id: $scope.id, ports: ports });
        };

        $scope.setName = function () {
            if ($scope.checkName()) {
                if ($scope.selectedIndex == 0)
                    Services.setName({ name: $scope.port.name, type: $scope.realType, Id: $scope.id });
                else
                    Services.changeOrderDate({
                        name: 'name', Id: $scope.id, type: $scope.realType,
                        value: $scope.port.name, index: $scope.index
                    });
                Services.boxCheck({ id: $scope.id });
            }
        };

        $scope.checkMarkerTime = function () {
            if ($scope.data.ETA_NORTime != undefined) {
                var date = $scope.data.ETA_NORTime;
                if ($scope.port.ETAMarker == 'NOR+6h' && $scope.data.ETAMarker != 'NOR+6h') {
                    date.setHours(date.getHours() + 6);
                    $scope.data.ETA_NORTime = date;
                    $scope.changePortDate('ETA_NORTime', $scope.getDateTimeString($scope.data.ETA_NORTime));
                    $scope.port.ETA_NORTime = $scope.getDateTimeString($scope.data.ETA_NORTime);
                    $scope.assignToAll('ETA_NORTimepopup', $scope.port.ETA_NORTime);
                    if ($scope.selectedIndex == 0)
                        $scope.assignToAll('ETA_NORTime' + $scope.realType + $scope.id, $scope.port.ETA_NORTime);
                }
                else if ($scope.port.ETAMarker != 'NOR+6h' && $scope.data.ETAMarker == 'NOR+6h') {
                    date.setHours(date.getHours() - 6);
                    $scope.data.ETA_NORTime = date;
                    $scope.changePortDate('ETA_NORTime', $scope.getDateTimeString($scope.data.ETA_NORTime));
                    $scope.port.ETA_NORTime = $scope.getDateTimeString($scope.data.ETA_NORTime);
                    $scope.assignToAll('ETA_NORTimepopup', $scope.port.ETA_NORTime);
                    if ($scope.selectedIndex == 0)
                        $scope.assignToAll('ETA_NORTime' + $scope.realType + $scope.id, $scope.port.ETA_NORTime);
                }
            }
            $scope.data.ETAMarker = $scope.port.ETAMarker;
        };

        $scope.changePortDate = function (name, date) {
            Services.changePortDate({id: $scope.id, name: name, date: $scope.getDateTimeString(date), type: $scope.realType});
        };

        $scope.checkName = function () {
            if ($scope.port.name == '') {
                $scope.hasError = true;
                return false;
            }
            for (var i = 0; i < $scope.ports.length; i++)
                if ($scope.ports[i].Name == $scope.port.name && i != $scope.index) {
                    $scope.hasError = true;
                    return false;
                }
            $scope.hasError = false;
            return true;
        };

        $scope.assignToAll = function (name, value) {
            var objs = document.getElementsByName(name);
            for (var i = 0; i < objs.length; i++)
                objs[i].value = value;
        };

        $scope.clearDate = function () {
            return null;
        };

        $scope.getData = function () {
            if ($scope.port.name.length == $scope.length) {
                $scope.currStyle = $scope.styleLoader;
                $http({
                    url: $scope.serviceUri + 'fixture/' + $scope.port.name,
                    method: "GET"
                }).success(function (data) {
                    var autocomplete = $("#name" + $scope.id).data("kendoAutoComplete");
                    if (autocomplete != null) {
                        autocomplete.setDataSource(new kendo.data.DataSource({
                            data: data
                        }));
                        autocomplete.search($scope.port.name);
                        $scope.currStyle = $scope.styleUnloader;
                    }
                }).error(function (data) {
                    $scope.currStyle = $scope.styleUnloader;
                });
            }
            else if ($scope.port.name.length < $scope.length) {
                var autocomplete = $("#name" + $scope.id).data("kendoAutoComplete");
                if (autocomplete != null)
                autocomplete.refresh();
            }
        };

        $scope.autocomplete = {
            dataTextField: "Name",
            minLength: 3,
            delay: 1000,
            select: function (e) {
                Services.changeOrderDate({
                    date: this.dataItem(e.item.index()), name: 'Zone',
                    type: $scope.realType, Id: $scope.id, index: $scope.index
                });
            }
            //dataSource: {
            //    type: "json",
            //    serverFiltering: true,
            //    transport: {
            //        read: {
            //            url: function (options) {
            //                return $scope.serviceUri + 'fixture';
            //            },
            //            cache: true,
            //            dataType: "json"
            //        },
            //        parameterMap: function (data, type) {
            //            if (type == "read") {
            //                return { name: $scope.port.name };
            //            }
            //        }
            //    }
            //}
        };
    });
})()